package cn.beatuty.makeup.module.system.dal.mysql.article;

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.*;

import cn.beatuty.makeup.framework.common.pojo.PageResult;
import cn.beatuty.makeup.framework.mybatis.core.query.LambdaQueryWrapperX;
import cn.beatuty.makeup.framework.mybatis.core.mapper.BaseMapperX;

import cn.beatuty.makeup.module.system.controller.admin.article.vo.ArticlePageReqVO;
import cn.beatuty.makeup.module.system.dal.dataobject.article.ArticleDO;
import cn.beatuty.makeup.module.system.dal.dataobject.hotproducts.HotProductsDO;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;

/**
 * 文章 Mapper
 *
 * @author 芋道源码
 */
@Mapper
public interface ArticleMapper extends BaseMapperX<ArticleDO> {
    default List<Map<String, Object>> countTotalPriceByDayInLast7Days() {
        String startDate = LocalDateTime.now().minusDays(7).format(DateTimeFormatter.ISO_LOCAL_DATE);
        startDate = startDate + " 00:00:00";
        QueryWrapper<ArticleDO> queryWrapper = new QueryWrapper<>();
        queryWrapper.select("DATE(create_time) AS date",  "COUNT(*) AS articleCount")  // 选择日期和总金额
                .ge("create_time", startDate)  // 限制日期在7天之前
                .groupBy("DATE(create_time)")  // 按日期分组
                .orderByAsc("DATE(create_time)");  // 按日期升序排序

        return this.selectMaps(queryWrapper);  // 使用 selectMaps 获取结果
    }


    default PageResult<ArticleDO> selectPage(ArticlePageReqVO reqVO) {
        return selectPage(reqVO, new LambdaQueryWrapperX<ArticleDO>()
                .eqIfPresent(ArticleDO::getTitle, reqVO.getTitle())
                .eqIfPresent(ArticleDO::getContent, reqVO.getContent())
                .eqIfPresent(ArticleDO::getCoverImage, reqVO.getCoverImage())
                .eqIfPresent(ArticleDO::getVideoUrl, reqVO.getVideoUrl())
                .eqIfPresent(ArticleDO::getStatus, reqVO.getStatus())
                .eqIfPresent(ArticleDO::getTopicId, reqVO.getTopicId())
                .eqIfPresent(ArticleDO::getTopicCode, reqVO.getTopicCode())
                .betweenIfPresent(ArticleDO::getCreateTime, reqVO.getCreateTime())
                .inIfPresent(ArticleDO::getAuthorId, reqVO.getAuthorIdReq())
                .orderByDesc(ArticleDO::getId));
    }
    @Select("select count(*) as totalarticles from app_article")
    String queryArticleTotal();

}